package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.AccountheadDao;
import io.renren.modules.sys.entity.AccountheadEntity;
import io.renren.modules.sys.service.AccountheadService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("accountheadService")
public class AccountheadServiceImpl extends ServiceImpl<AccountheadDao, AccountheadEntity> implements AccountheadService {

    @Autowired
    AccountheadDao accountheadDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<AccountheadEntity> page = this.selectPage(
                new Query<AccountheadEntity>(params).getPage(),
                new EntityWrapper<AccountheadEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String getMaxId() {
        return accountheadDao.getMaxId();
    }

    @Override
    public Double findAllMoney(String getS, String type, String mode,String endTime) {
        String modelName = "";
        if (mode.equals("实际")) {
            modelName = "ChangeAmount";
        } else if (mode.equals("合计")) {
            modelName = "TotalPrice";
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("organId",getS);
        map.put("type",type);
        map.put("modelName",modelName);
        map.put("endTime",endTime);
        return accountheadDao.findAllMoney(map);
    }

    @Override
    public JSONObject findFinancialInfo(Map params) {
        int pageNo = Integer.parseInt(params.get("pageNo") + "");
        int pageSize = Integer.parseInt(params.get("pageSize") + "");
        params.put("pageNo", pageNo-1);
        params.put("pageSize", pageSize);
        params.put("order", "desc");
        List<AccountheadEntity> dataList = accountheadDao.findFinancialInfo(params);
        JSONObject outer = new JSONObject();
        outer.put("total", dataList.size());
        JSONArray dataArray = new JSONArray();
        if (null != dataList) {
            for (AccountheadEntity accountHead : dataList) {
                JSONObject item = new JSONObject();
                item.put("Id", accountHead.getId());
                item.put("OrganId", accountHead.getOrganid() == null ? "" : accountHead.getOrganid());
                item.put("OrganName", accountHead.getOrganid() == null ? "" : supplierQry(accountHead.getOrganid()));
                item.put("HandsPersonId", accountHead.getHandspersonid() == null ? "" : accountHead.getHandspersonid());
                item.put("HandsPersonName", accountHead.getHandspersonid() == null ? "" : personQry(accountHead.getHandspersonid()));
                item.put("AccountId", accountHead.getAccountid() == null ? "" : accountHead.getAccountid());
                item.put("AccountName", accountHead.getAccountid() == null ? "" : accountQry(accountHead.getAccountid()));
                item.put("BillNo", accountHead.getBillno());
                item.put("BillTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(accountHead.getBilltime()));
                item.put("ChangeAmount", accountHead.getChangeamount() == null ? "" : Math.abs(accountHead.getChangeamount()));
                item.put("TotalPrice", accountHead.getTotalprice() == null ? "" : Math.abs(accountHead.getTotalprice()));
                item.put("Remark", accountHead.getRemark());
                item.put("op", 1);
                dataArray.add(item);
            }
        }
        outer.put("rows", dataArray);
        return outer;
    }

    private String accountQry(Long id){
        return accountheadDao.accountQry(id);
    }

    private String supplierQry(Long id){
        return accountheadDao.supplierQry(id);
    }

    private String personQry(Long id){
        return accountheadDao.personQry(id);
    }
}
